package base.lib.widget.Floating;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.GridView;

import java.lang.reflect.Field;

import base.lib.R;

public class AppNoScrollGridView extends GridView {

    private ColorDrawable mDivider;
    private int mDividerWidth;
    private boolean isDrawDivider;

    public AppNoScrollGridView(Context context) {
        super(context);
    }

    public AppNoScrollGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AppNoScrollGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

    /**
     * 如果是低于11的版本,采用反射获取列数
     * @return
     */
    @SuppressLint("NewApi")
    private int getColumns(){
        int columns = -1;
        if(Build.VERSION.SDK_INT >Build.VERSION_CODES.GINGERBREAD_MR1){
            columns = getNumColumns();
        }else{
            try {
                Field field = this.getClass().getDeclaredField("mNumColumns");
                columns = field.getInt(this);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        if(columns == -1){
            columns = 3; //部分被修改的Android版本,没有mNumColumns字段
        }
        return columns;
    }

    public boolean isDrawDivider(){
        return isDrawDivider;
    }
    public void setIsDrewDivider(boolean isDraw){
        isDrawDivider = isDraw;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(isDrawDivider){
            drawDivider(canvas);
        }
    }

    /**
     * 空位也绘制
     * @param canvas
     */
    protected void drawDivider(Canvas canvas){
        if(mDivider == null){
            Resources res = getContext().getResources();
            int color = res.getColor(R.color.app_bg);
            mDividerWidth = (int)res.getDimension(R.dimen.dimen_1);
            mDivider = new ColorDrawable(color);
        }
        int numColumns = getColumns();

        int count = getChildCount();
        int remaind = count % numColumns;

        for(int i = 0; i< count - remaind;i++){
//                View child = getChildAt(i);
//                drawHorizontalDivider(canvas,child);
//                drawVerticalDivider(canvas,child);

            // 绘制竖线
            if(i < numColumns && numColumns -1 != i % numColumns){
                View child = getChildAt(i);
                if(count > numColumns){
                    drawVerticalDivider(canvas,child);
                }else{
                    drawVerticalDivider(canvas, child);
                }
            }
            //绘制横线
            if(i == 0){
                View child = getChildAt(i);
                drawHorizontalDividerWidthChild(canvas,child);
            }else if(i % numColumns == 0){
                View child = getChildAt(i);
                drawHorizontalDivider(canvas,child);
            }
        }
    }

    /**
     * 空位不绘制
     * @param canvas
     */
    protected void drawDividerWithoutEmpty(Canvas canvas){
        if(mDivider == null){
            Resources res = getContext().getResources();
            int color = res.getColor(R.color.app_bg);
            mDividerWidth = (int)res.getDimension(R.dimen.dimen_1);
            mDivider = new ColorDrawable(color);
        }
        int numColumns = getColumns();

        int count = getChildCount();
        int remaind = count % numColumns;

        for(int i = 0; i< count - remaind;i++){
//                View child = getChildAt(i);
//                drawHorizontalDivider(canvas,child);
//                drawVerticalDivider(canvas,child);

            //绘制横线
            if(i == 0){
                View child = getChildAt(i);
                drawHorizontalDividerWidthChild(canvas,child);
            }else if(i % numColumns == 0){
                View child = getChildAt(i);
                drawHorizontalDivider(canvas,child);
            }
            // 绘制竖线
            if(i < numColumns && numColumns -1 != i % numColumns){
                View child = getChildAt(i);
                if(count > numColumns){
                    drawVerticalDivider(canvas,child);
                }else{
                    drawVerticalDivider(canvas, child);
                }
            }
        }
    }

    /**
     * 绘制整条的横线
     * @param canvas
     * @param child
     */
    private void drawHorizontalDivider(Canvas canvas,View child){

        int left = 0;
        int right = getRight();
        int top = child.getBottom();
        int bottom = top + mDividerWidth;
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(canvas);

    }

    /**
     * 绘制一个Child的横线
     * @param canvas
     * @param child
     */
    private void drawHorizontalDividerWidthChild(Canvas canvas,View child){

        int left = 0;
        int right = getRight();
        int bottom = child.getBottom();
        int top = bottom - mDividerWidth;
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(canvas);

    }

    /**
     * 绘制整条的竖线
     * @param canvas
     * @param child
     */
    private void drawVerticalDivider(Canvas canvas,View child){
        int left = child.getRight();
        int right = left + mDividerWidth;
        int top = 0;
        int bottom = getHeight();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(canvas);
    }

    /**
     * 绘制一个Child的竖线
     * @param canvas
     * @param child
     */
    private void drawVerticalDividerWidthChild(Canvas canvas,View child){
        int left = child.getRight();
        int right = left + mDividerWidth;
        int top = child.getTop();
        int bottom = child.getBottom();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(canvas);
    }
}
